﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PJ.Framework
{
    public static class ListUtil
    {
        /// <summary>
        /// Creates a list
        /// </summary>
        /// <typeparam name="TObject">Object type</typeparam>
        /// <param name="order">List order</param>
        /// <param name="list1">First list. Can be null.</param>
        /// <param name="list2">Second list. Can be null.</param>
        /// <returns>List of items. Empty if no items found</returns>
        /// <exception cref="ArgumentNullException">If the passed <paramref name="order"/> is <see langword="null" /> or empty.</exception>
        public static List<TObject> Fusion<TObject>(string[] order, Dictionary<string, TObject> list1, Dictionary<string, TObject> list2)
            where TObject : class
        {
            if (order == null || order.Count() == 0)
                throw new ArgumentNullException("order");
            List<TObject> list = new List<TObject>();
            foreach (string key in order)
            {
                TObject obj = null;
                if (list1 != null)
                    list1.TryGetValue(key, out obj);
                if (list2 != null && obj == null)
                    list2.TryGetValue(key, out obj);
                if (obj != null)
                    list.Add(obj);
            }
            return list;
        }
    }
}
